package com.ruoyi.jihuo.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jihuo.config.TaskManager;
import com.ruoyi.jihuo.domain.JhTmTask;
import com.ruoyi.jihuo.domain.TrafficCount;
import com.ruoyi.jihuo.enums.JhTmTaskStatus;
import com.ruoyi.jihuo.service.IJhTmTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发布天猫任务Controller
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/jihuo/task")
public class JhTmTaskController extends BaseController {
    @Autowired
    private IJhTmTaskService jhTmTaskService;
    @Autowired
    private TaskManager taskManager;

    /**
     * 查询发布天猫任务列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(JhTmTask jhTmTask) {
        startPage();
        List<JhTmTask> list = jhTmTaskService.selectJhTmTaskList(jhTmTask);
        return getDataTable(list);
    }

    /**
     * 导出发布天猫任务列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:export')")
    @Log(title = "发布天猫任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JhTmTask jhTmTask) {
        List<JhTmTask> list = jhTmTaskService.selectJhTmTaskList(jhTmTask);
        ExcelUtil<JhTmTask> util = new ExcelUtil<JhTmTask>(JhTmTask.class);
        util.exportExcel(response, list, "发布天猫任务数据");
    }

    /**
     * 获取发布天猫任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:query')")
    @GetMapping(value = "/{jhId}")
    public AjaxResult getInfo(@PathVariable("jhId") Long jhId) {
        return success(jhTmTaskService.selectJhTmTaskByJhId(jhId));
    }

    /**
     * 新增发布天猫任务
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:add')")
    @Log(title = "发布天猫任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JhTmTask jhTmTask) {
        return toAjax(jhTmTaskService.insertJhTmTask(jhTmTask));
    }

    /**
     * 修改发布天猫任务
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:edit')")
    @Log(title = "发布天猫任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JhTmTask jhTmTask) {
        return toAjax(jhTmTaskService.updateJhTmTask(jhTmTask));
    }

    /**
     * 删除发布天猫任务
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:remove')")
    @Log(title = "发布天猫任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jhIds}")
    public AjaxResult remove(@PathVariable String[] jhIds) {
        return toAjax(jhTmTaskService.deleteJhTmTaskByJhIds(jhIds));
    }

    /**
     * 新增发布天猫任务
     */
    @PreAuthorize("@ss.hasPermi('jihuo:task:start')")
    @Log(title = "发布天猫任务", businessType = BusinessType.OTHER)
    @PostMapping("/start/{jhId}")
    public AjaxResult start(@PathVariable("jhId") Long jhId) {
        try {
            jhTmTaskService.start(jhId);
        }catch (Exception e){
            return AjaxResult.error("发布失败");
        }
        return AjaxResult.success();
    }

    /**
     * 重新发布天猫任务
     * @param jhId
     * @return
     */
    @Log(title = "重新发布天猫任务", businessType = BusinessType.OTHER)
    @PostMapping("/againStart/{jhId}")
    public AjaxResult againStart(@PathVariable("jhId") Long jhId) {
        try {
            jhTmTaskService.againStart(jhId);
        }catch (Exception e){
            return AjaxResult.error("重新发布失败");
        }
        return AjaxResult.success();
    }

    /**
     * 取消任务
     * @param taskId
     * @return
     */
    @PostMapping("/cancel/{taskId}")
    public AjaxResult cancelTask(@PathVariable String taskId) {
        try {
            taskManager.cancelTask(taskId);
            jhTmTaskService.updateJhStatus(taskId, JhTmTaskStatus.CANCEL.toString());
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(true);
    }

}
